<?php

setlocale(LC_ALL, "ru_RU.UTF-8");
mb_internal_encoding("UTF-8");

require_once ("./config/setup.php");
require_once ("./libs/db.class.php");
require_once ("./libs/utils.inc.php");
$sm = new SSmarty();
$db = new DB();

$curRealDir = mb_substr($_SERVER["SCRIPT_FILENAME"], 0, mb_strrpos($_SERVER["SCRIPT_FILENAME"], "/"));
$curSiteDir = "http://" . $_SERVER["SERVER_NAME"] . mb_substr($_SERVER["PHP_SELF"], 0, mb_strrpos($_SERVER["PHP_SELF"], "/"));
$curPage = mb_substr($_SERVER["SCRIPT_NAME"], mb_strrpos($_SERVER["SCRIPT_NAME"], "/") + 1);

$sm->assign("curSiteDir", $curSiteDir);
$sm->assign("curPage", $curPage);

if (!isset($_GET["action"])) {
    if (preg_match("'^[\d]+$'iusx", $_GET["newid"])) {
        $db->dbq("
                    Select
                        commentid as commentid,
                        username as username
                    From
                        comments
                    Where
                        newid = \"" . $_GET["newid"] . "\"
                    Order By
                        postdate DESC
                ");
    } else {
        $db->dbq("
                    Select
                        commentid as commentid,
                        username as username
                    From
                        comments
                    Order By
                        postdate DESC
                ");
    }


    $currentComments = array();
    while ($arr = $db->assoc()) {
        $currentComments[] = $arr;
    }

    $currentComments = stripRecursive($currentComments);

    $sm->assign("currentComments", $currentComments);
} elseif ($_GET["action"] == "change") {
    if (preg_match("'^[0-9]+'$iu", $_GET["commentid"])) {
        $db->dbq("
                        Select
                            *
                        From
                            comments
                        Where
                            commentid = \"" . $_GET["commentid"] . "\"
                    ");
        $currentComment = $db->assoc();
        $currentComment = stripRecursive($currentComment);

        $sm->assign("currentComment", $currentComment);
    } else {
        $sm->assign("redirectPage", $curPage);
        $sm->assign("error", "Неверное значение идентификатора комментария");
        $sm->display("reset.html");
        exit(0);
    }
} elseif ($_GET["action"] == "delete") {
    if (preg_match("'^[0-9]+'$iu", $_GET["commentid"])) {

        $db->dbq("
                        Select
                            commentid as commentid,
                            username as username
                        From
                            comments
                        Where
                            commentid = \"" . $_GET["commentid"] . "\"
                    ");
        $currentComment = $db->assoc();

        $currentComment = stripRecursive($currentComment);

        $sm->assign("currentComment", $currentComment);
    } else {
        $sm->assign("redirectPage", $curPage);
        $sm->assign("error", "Неверное значение идентификатора комментария");
        $sm->display("reset.html");
        exit(0);
    }
}

if (isset($_POST["commentChangeEnter"])) {
    $_POST = escapeRecursive($_POST);

    $db->dbq("
                    Update
                        comments
                    Set
                        username = \"" . $_POST["username"] . "\",
                        maintext = \"" . $_POST["maintext"] . "\"
                    Where
                        commentid = \"" . $_POST["commentid"] . "\"
                 ");

    $sm->assign("redirectPage", "comments.php?" . $_SERVER["QUERY_STRING"]);
    $sm->display("reset.html");
    exit(0);
} elseif (isset($_POST["commentDeleteEnter"])) {
    $_POST = escapeRecursive($_POST);

    $db->dbq("
                    Delete
                    From
                        comments
                    Where
                        commentid = \"" . $_POST["commentid"] . "\"
                 ");

    $sm->assign("redirectPage", "comments.php?" . $_SERVER["QUERY_STRING"]);
    $sm->display("reset.html");
    exit(0);
}

$sm->assign("design", $sm->template_dir);
$sm->display("comments.html");
?>